//
//  NEDFormItem+Add.m
//  NEDFormView_Example
//
//  Created by 000 on 2022/1/8.
//  Copyright © 2022 000. All rights reserved.
//

#import "NEDFormItem+Add.h"
#import <NEDConstant/NEDConstant.h>
#import "NEDFormContentItem+Privite.h"

@implementation NEDFormItem (Add)

/// MARK: - left right center

- (NEDFormItem *(^)(NEDFormContentShowType showType))initLeft{
    NEDFormItem *(^block)(NEDFormContentShowType showType) = ^NEDFormItem *(NEDFormContentShowType showType) {
        self.leftContent = [[NEDFormContentItem alloc] initWithType:showType position:(NEDFormContentShowPosition_Left) belongFormSectionItem:self];
        return self;
    };
    return block;
}
- (NEDFormItem *(^)(NEDFormContentShowType showType))initRight{
    NEDFormItem *(^block)(NEDFormContentShowType showType) = ^NEDFormItem *(NEDFormContentShowType showType) {
        self.rightContent = [[NEDFormContentItem alloc] initWithType:showType position:(NEDFormContentShowPosition_Right) belongFormSectionItem:self];
        return self;
    };
    return block;
}
- (NEDFormItem *(^)(NEDFormContentShowType showType))initCenter{
    NEDFormItem *(^block)(NEDFormContentShowType showType) = ^NEDFormItem *(NEDFormContentShowType showType) {
        self.centerContent = [[NEDFormContentItem alloc] initWithType:showType position:(NEDFormContentShowPosition_Center) belongFormSectionItem:self];
        return self;
    };
    return block;
}

- (NEDFormContentItem *)left{
    if(self.__avaiableLeft){
        return self.leftContent;
    }
    return nil;
}
- (NEDFormContentItem *)center{
    if(self.__avaiableCenter){
        return self.centerContent;
    }
    return nil;
}
- (NEDFormContentItem *)right{
    if(self.__avaiableRight){
        return self.rightContent;
    }
    return nil;
}


/// MARK: - init view

- (void)initViewLeftBlock:(NEDFormContentInitViewBlock)initView{
    if(!self.__avaiableLeft){
        return;
    }
    [self.leftContent setInitViewBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(initView){
            return  initView(sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
        return nil;
    }];
}
- (void)initViewRightBlock:(NEDFormContentInitViewBlock)initView{
    if(!self.__avaiableRight){
        return;
    }
    [self.rightContent setInitViewBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(initView){
            return  initView(sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
        return nil;
    }];
}
- (void)initViewCenterBlock:(NEDFormContentInitViewBlock)initView{
    if(!self.__avaiableCenter){
        return;
    }
    [self.centerContent setInitViewBlock:^UIView * _Nonnull(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(initView){
            return  initView(sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
        return nil;
    }];
}

/// MARK: - style

- (void)styleLeftBlock:(NEDFormContentViewBlock)style{
    if(!self.__avaiableLeft){
        return;
    }
    [self.leftContent setStyleBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(style){
            style(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}
- (void)styleRightBlock:(NEDFormContentViewBlock)style{
    if(!self.__avaiableRight){
        return;
    }
    [self.rightContent setStyleBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(style){
            style(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}
- (void)styleCenterBlock:(NEDFormContentViewBlock)style{
    if(!self.__avaiableCenter){
        return;
    }
    [self.centerContent setStyleBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(style){
            style(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}

/// MARK: - show

- (void)showLeftBlock:(NEDFormContentViewBlock)show{
    if(!self.__avaiableLeft){
        return;
    }
    [self.leftContent setShowViewBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(show){
            show(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}
- (void)showRightBlock:(NEDFormContentViewBlock)show{
    if(!self.__avaiableRight){
        return;
    }
    [self.rightContent setShowViewBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(show){
            show(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}
- (void)showCenterBlock:(NEDFormContentViewBlock)show{
    if(!self.__avaiableCenter){
        return;
    }
    [self.centerContent setShowViewBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(show){
            show(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}

/// MARK: - available

- (void)availableLeftBlock:(NEDFormContentAvailableBlock)available{
    if(!self.__avaiableLeft){
        return;
    }
    [self.leftContent setAvailableBlock:^BOOL(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(available){
            return available(sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
        return YES;
    }];
}
- (void)availableRightBlock:(NEDFormContentAvailableBlock)available{
    if(!self.__avaiableRight){
        return;
    }
    [self.rightContent setAvailableBlock:^BOOL(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(available){
            return available(sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
        return YES;
    }];
}
- (void)availableCenterBlock:(NEDFormContentAvailableBlock)available{
    if(!self.__avaiableCenter){
        return;
    }
    [self.centerContent setAvailableBlock:^BOOL(NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(available){
            return available(sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
        return YES;
    }];
}

/// MARK: - click

- (void)clickLeftBlock:(NEDFormContentViewBlock)click{
    if(!self.__avaiableLeft){
        return;
    }
    [self.leftContent setClickBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(click){
            click(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}
- (void)clickRightBlock:(NEDFormContentViewBlock)click{
    if(!self.__avaiableRight){
        return;
    }
    [self.rightContent setClickBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(click){
            click(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}
- (void)clickCenterBlock:(NEDFormContentViewBlock)click{
    if(!self.__avaiableCenter){
        return;
    }
    [self.centerContent setClickBlock:^(__kindof UIView * _Nonnull contentView, NEDFormItem * _Nonnull sectionItem, NEDFormContentItem * _Nonnull contentItem) {
        if(click){
            click(contentView,sectionItem?:contentItem.__belongFormSectionItem,contentItem);
        }
    }];
}

/// MARK: - get view 获取相应的视图

- (UIView * _Nullable)leftView{
    if(!self.__avaiableLeft){
        return nil;
    }
    if(self.leftContent.getViewBlock){
        return self.leftContent.getViewBlock();
    }
    return nil;
}
- (UIView * _Nullable)rightView{
    if(!self.__avaiableRight){
        return nil;
    }
    if(self.rightContent.getViewBlock){
        return self.rightContent.getViewBlock();
    }
    return nil;
}
- (UIView * _Nullable)centerView{
    if(!self.__avaiableCenter){
        return nil;
    }
    if(self.centerContent.getViewBlock){
        return self.centerContent.getViewBlock();
    }
    return nil;
}

/// MARK: - 定义 right view

///// 显示右侧箭头
//- (NEDFormItem *)showRightArrow{
//    if(self.rightContent){
//        NSLogError(@"已有右侧 视图布局");
//        return self;
//    }
//    
//}


/// MARK: - layout

- (NEDFormItem *(^)(CGFloat hegiht))height{
    NEDFormItem *(^block)(CGFloat hegiht) = ^NEDFormItem *(CGFloat hegiht) {
        self.layout.height = hegiht;
        return self;
    };
    return block;
}
- (NEDFormItem *(^)(CGFloat width))widthLeft{
    NEDFormItem *(^block)(CGFloat width) = ^NEDFormItem *(CGFloat width) {
        self.layout.widthContentLeft = width;
        return self;
    };
    return block;
}
- (NEDFormItem *(^)(CGFloat width))widthRight{
    NEDFormItem *(^block)(CGFloat width) = ^NEDFormItem *(CGFloat width) {
        self.layout.widthContentRight = width;
        return self;
    };
    return block;
}
- (NEDFormItem *(^)(CGFloat space))marginLeft{
    NEDFormItem *(^block)(CGFloat space) = ^NEDFormItem *(CGFloat space) {
        self.layout.marginLeft = space;
        return self;
    };
    return block;
}
- (NEDFormItem *(^)(CGFloat space))marginRight{
    NEDFormItem *(^block)(CGFloat space) = ^NEDFormItem *(CGFloat space) {
        self.layout.marginRight = space;
        return self;
    };
    return block;
}

- (NEDFormItem *(^)(UIColor * color))backgroundColor{
    NEDFormItem *(^block)(UIColor * color) = ^NEDFormItem *(UIColor * color) {
        self.layout.backgroundColor = color;
        return self;
    };
    return block;
}

- (NEDFormItem *(^)(NSInteger type))bindType{
    NEDFormItem *(^block)(NSInteger type) = ^NEDFormItem *(NSInteger type) {
        self.ID = @(type).stringValue;
        return self;
    };
    return block;
}

#pragma mark -


- (void)configLayout:(void(^)(NEDFormSectionLayout * layout))layoutConfig{
    if(layoutConfig){
        layoutConfig(self.layout);
    }
}

- (NEDFormItem *)showLineTop{
    self.layout.showTopSeparatorLine = YES;
    return self;
}
- (NEDFormItem *)hiddenLineBottom{
    self.layout.showBottomSeparatorLine = NO;
    return self;
}
- (NEDFormItem *)showRightArrow{
    self.layout.showRightArrowView = YES;
    return self;
}

#pragma mark - privite

- (BOOL)__avaiableLeft{
    if(self.leftContent){
        return YES;
    }
    NSLogError(@"请先执行 initLeft");
    return NO;
}
- (BOOL)__avaiableCenter{
    if(self.centerContent){
        return YES;
    }
    NSLogError(@"请先执行 initCenter");
    return NO;
}
- (BOOL)__avaiableRight{
    if(self.rightContent){
        return YES;
    }
    NSLogError(@"请先执行 initRight");
    return NO;
}


@end



@implementation NEDFormContentItem (Add)

- (NEDFormItem *(^)(NSString * title))title{
    NEDFormItem *(^block)(NSString * title) = ^NEDFormItem *(NSString * title) {
        self.contentTitle = title;
        return self.__belongFormSectionItem;
    };
    return block;
}
- (NEDFormItem *(^)(NSString * imageURL))imageURL{
    NEDFormItem *(^block)(NSString * imageURL) = ^NEDFormItem *(NSString * imageURL) {
        self.contentImageURL = imageURL;
        return self.__belongFormSectionItem;
    };
    return block;
}
- (NEDFormItem *(^)(BOOL isOn))isOn{
    NEDFormItem *(^block)(BOOL isOn) = ^NEDFormItem *(BOOL isOn) {
        self.contentSwitchIsOn = isOn;
        return self.__belongFormSectionItem;
    };
    return block;
}
- (NEDFormItem *(^)(id dataInfo))linkDataInfo{
    NEDFormItem *(^block)(id dataInfo) = ^NEDFormItem *(id dataInfo) {
        self.contentLinkDataInfo = dataInfo;
        return self.__belongFormSectionItem;
    };
    return block;
}

@end
